//
//  AFGetData.h
//  MVideo
//
//  Created by Lin Eidan on 14-3-21.
//  Copyright (c) 2014年 taomee. All rights reserved.
//

#import "AFGetData.h"

#pragma -mark 目前自己有修改的文件有：AFURLSessionManager.m

//ios6
typedef void (^downloadProgessBlock)  (AFDownloadRequestOperation *operation,float percent,long long totalBytesReadForFile,long long totalBytesExpectedToReadForFile);
typedef void (^downloadCompletionBlock) (AFHTTPRequestOperation *operation, long long contentLength, BOOL isSuccess);  //void 返回值，后面的为参数

//ios7
typedef void (^downloadCompletionGetDataTaskBlock) (NSURLResponse *response, id responseObj, BOOL isSuccess);  //实际上是请求数据，但要用下载的方式去请求，因为这样才能后台
typedef void (^downloadProgessTaskBlock)  (NSURLSessionDownloadTask *task,float percent);
typedef void (^downloadCompletionTaskBlock) (NSURLResponse *response, long long contentLength, BOOL isSuccess);  //void 返回值，后面的为参数


@interface DownloadGetData : AFGetData

+ (DownloadGetData *)sharedInstance;


//ios6 下载
-(AFDownloadRequestOperation *)startDownload:(NSString *)url andFilePath:(NSString *)filePath andAllowResume:(BOOL)allowResume andProgress:(downloadProgessBlock)progessBlock andCompletion:(downloadCompletionBlock)block;



//ios7 前台下载,一般不用
-(NSURLSessionDownloadTask *)startTaskDownload:(NSString *)url andFileFolderPath:(NSString *)fileFolderPath andFileName:(NSString *)fileName andAllowResume:(BOOL)allowResume andStartRightNow:(BOOL)rightRow andResumeData:(NSData *)resumeData andProgress:(downloadProgessTaskBlock)progessBlock andCompletion:(downloadCompletionTaskBlock)block;



//ios7 background
@property (strong, nonatomic) AFURLSessionManager *sessionBackManager;

//后台请求数据,结果如果是JSON，就传YES，responseObj就为JSON，如果不是，就传NO，responseObj就为NSData
-(NSURLSessionDownloadTask *)startTaskBackgroundDataGet:(NSString *)url andFileFolderPath:(NSString *)fileFolderPath andFileName:(NSString *)fileName andResultIsJSON:(BOOL)iSJSON andTaskCompletion:(downloadCompletionGetDataTaskBlock)block;

//后台下载
-(NSURLSessionDownloadTask *)startTaskBackgroundDownload:(NSString *)url andFileFolderPath:(NSString *)fileFolderPath andFileName:(NSString *)fileName andAllowResume:(BOOL)allowResume andStartRightNow:(BOOL)rightRow andResumeData:(NSData *)resumeData andProgress:(downloadProgessTaskBlock)progessBlock andCompletion:(downloadCompletionTaskBlock)block;




//取消请求
-(void)removeAllDownloadRelatedRequest;

//上次不正常退出，如闪退，下次进来后要先处理一下，让ResumeData有。
-(void)handleResumeDataWhenLastCrashWith:(id )delegate andTask:(NSURLSessionTask *)task andError:(NSError *)error;

//删除取消下载的缓存文件
-(void)deleteTheTempFileWithResumeData:(NSData *)resumeData;

@end
